﻿using RPCServer.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace RPCServer.Network
{
    [MObject]
    class UserToken
    {
        //Socket _socket;

        //public Socket Socket 
        //{
        //    get => _socket;
        //    set
        //    {
        //        if (value != null)
        //            _socket = value;
        //        else
        //            throw new NullReferenceException("Socket 不能为 null");
        //    }
        //}

        //bool _isDisposed;
        //public bool IsDisposed { get => _isDisposed; }
        //public bool IsDisposed { get; set; }

        Socket _socket;
        public Socket Socket
        {
            get => _socket;
            set
            {
                _socket = value;
                IP = value == null ? null : _socket.RemoteEndPoint;
            }
        }

        public EndPoint IP { get; private set; }

        public long LastTicks { get; set; }

        public bool IsClose { get; set; }

        public MBufferPacket Parser { get; set; }

        public void Clear()
        {
            if (!IsClose)
            {
                Socket?.Close();
            }
            Socket = null;
            LastTicks = long.MinValue;
            IsClose = true;
            Parser = null;
        }

    }
}
